import { _decorator, Node, RigidBody, Vec3 } from 'cc';
import { HitBallAttribute } from '../core/Game';
import { BallAttribute, BallColor } from '../core/BallAttribute';
import { PlayUser } from '../core/PlayUser';
import { NetBall } from '../core/WebSocketData';
import { TableAtrribute } from '../core/TableAttribute';

export class BallGameBase {
  public billiamNode: Node = null;
  constructor(bn: Node) { 
    this.billiamNode = bn;
  }
  // 初始化球的位置
  public initBallsPosition() { }
  // 初始状态时，瞄准球的位置
  public defaultAimBallPosition() {
    return new Vec3(0, TableAtrribute.DeskTopAxisY, 0);
  }
  // 游戏是否已经结束
  public checkGameEnd() {
    return false;
  }
  // 是否进球得分
  public hasHoledScore(hitBallList: Array<HitBallAttribute>, step: number, color: BallColor) {
    return false;
  }
  // 是否违规击球
  public isIllegalHitBall(firstCollideBall: BallAttribute, hitBallList: Array<HitBallAttribute>, step: number, color: BallColor) {
    return { isIlleagel: false, message: 'ok' };
  }
  public isTargetHitBall(color: BallColor, name: string) {
    return true;
  }
  // 当前击球 和受保护 的球列表
  public getHitProtectBalls(color: BallColor) {

  }
  public getUserHitColorText(color: BallColor) {
    // return color.getColorText();
    return '';
  }
  public getCurrentStepScore(hitBallList: Array<HitBallAttribute>, step: number, color: BallColor) {

  }
  public penalyScore() {
    return 4;
  }
  public swapCurrentUserColor(hasExchangeUser: boolean) {

  }
  public isCurrentUserWin(side: number, isIlleagel: boolean) {

  }
  public checkOutHoleBalls(isIgnoreColorStage: boolean, isIllegalHitBall: boolean, currentHitBalls: Array<HitBallAttribute>) {

  }
  public isExcellentLevel(hitBallList: Array<HitBallAttribute>, step: number, color: BallColor, userId: string) {

  }
  public currentHitGanCount() {
    return 0;
  }
  public robotRenShu(playUsers: PlayUser[]) { }
  // 所有球在网络中的信息
  protected getNetFrameBallList(mainBallNode: Node, ballNodes: Node[]) {
    const bList: Array<NetBall> = [];
    const mbb = mainBallNode.getComponent(RigidBody);
    var pos = new Vec3();
    var vel = new Vec3();
    var ang = new Vec3();
    mainBallNode.getPosition(pos);
    mbb.getLinearVelocity(vel);
    // active 表示最终是否进球了
    const ball = new NetBall(mainBallNode.name, pos, vel, mainBallNode.active);
    bList.push(ball);
    
    for (const b of ballNodes) {
      const bb = b.getComponent(RigidBody);
      bb.getLinearVelocity(vel);
      pos = b.getPosition();
      const ball = new NetBall(b.name, pos, vel, b.active);
      bb.getAngularVelocity(ang);
      ball.setAngVel(ang);
      bList.push(ball);
    }
    return bList;
  }
  // 是否违规击球, 以及结果信息
  protected illeageHitBallResult(firstCollideBall: BallAttribute, hitBallList: Array<HitBallAttribute>, step: number, color: BallColor) {
    // 1. 白球进洞
    for (var i = hitBallList.length -1; i>=0; i--) {
      const hba = hitBallList[i];
      if (hba.step === step) {
        if (hba.ba.isMainWhiteBall()) {
          return { isIlleagel: true, message: '白球进洞，违规击球' };
        }
      }
    }
    // 2. 白球没有碰到的球
    if (!firstCollideBall) {
      return { isIlleagel: true, message: '未击中球，违规击球' };
    }
    // 3. 白球首次碰到的不是自己的球
    if (firstCollideBall && color.isSureColored() && !color.isSpecialColor()) {
      if (firstCollideBall.color !== color.color) {
        return { isIlleagel: true, message: '未先击中目标球，违规击球' };
      }
    }
    return { isIlleagel: false, message: 'ok' };
  }
}